dofile(os.getenv('LUAR_HOME') .. '/util/Xml.lua')
require 'posix'

function includeTemplate( name )

	local file = File{}
	local templatecode = file:readFile( os.getenv('LUAR_HOME') .. '/lib/templates/' .. name .. '/main.luar'  )
	
	local app_proc = ApplicationProcessor{}

	copyFiles( 'templates', name )

	local ret = app_proc:processApplication( templatecode )
	
	return ret
end

function includeComponent( name )
	
	local file = File{}
	local component_code = file:readFile( os.getenv('LUAR_HOME') .. '/lib/components/' .. name .. '/main.luar'  )
	
	local app_proc = ApplicationProcessor{}
	
	local  component_code = app_proc:processApplication( component_code )  
	
	copyFiles( 'components', name )

	local xml = Xml{}
	return xml:buildNclTree( component_code )
	
end

function copyFiles( dir, name )
	local file = File{}
	
	if file:exists( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/audio', 'target' ) then
		if not file:isEmpty( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/audio', 'target' ) then
			file:copy( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/audio', 'target' )
		end
	end

	if file:exists( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/image' ) then
		if not file:isEmpty( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/image' ) then
			file:copy( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/image', 'target' )
		end
	end

	if file:exists( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/lua' ) then
		if not file:isEmpty( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/lua' ) then
			file:copy( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/lua', 'target' )
		end
	end

	if file:exists( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/text' ) then
		if not file:isEmpty( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/text' ) then
			file:copy( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/text', 'target' )
		end
	end

	if file:exists( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/video' ) then
		if not file:isEmpty( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/video' ) then
			file:copy( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/video', 'target' )
		end
	end

	if file:exists( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/ncl' ) then
		if not file:isEmpty( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/ncl' ) then
			file:copy( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/ncl', 'target' )
		end
	end

	if file:exists( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/others' ) then
		if not file:isEmpty( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/others' ) then
			file:copy( os.getenv('LUAR_HOME') .. '/lib/' .. dir .. '/' .. name .. '/others', 'target' )
		end
	end
end

function createLuaScript(filename, content)
	local file = File{};
	file:createNewFile('media/lua/'..filename);
	file:writeFile('media/lua/'..filename, content);
end

function image(filename)
	if filename == nil then
		print('Image filename is nil');
		return;
	end
	return 'media/image/'..filename;
end

function video(filename)
	if filename == nil then
		print('Video filename is nil');
		return;
	end
	return 'media/video/'..filename;
end

function text(filename)
	if filename == nil then
		print('Text filename is nil');
		return;
	end
	return 'media/text/'..filename;
end

function lua(filename)
	if filename == nil then
		print('Lua Script filename is nil');
		return;
	end
	return 'media/lua/'..filename;
end


function audio(filename)
	if filename == nil then
		print('Audio filename is nil');
		return;
	end
	return 'media/audio/'..filename;
end

function luar_error(template_name, message)
	print('Erro to process '..template_name..': '..message)
	print('Fails to perform operation')
	os.exit(1)
end
